package com.zhenyi.youqu.admin.index;

import java.sql.SQLException;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.youqu.common.model.YqApp;
import com.youqu.common.model.YqShop;
import com.youqu.common.model.YqShopUser;

/**
 * 商铺管理
 * 
 * @author cen
 *
 */
public class ShopController extends YQAdminController {

	@Override
	public void list() {
		Kv kv = getSimpleKv();
		YqShopUser user = getShopUser();
		if (user.getRole().equals(YqShopUser.ROLE_ADMIN)) {
			kv.set("appId", user.getAppId());
		} else if (user.getRole().equals(YqShopUser.ROLE_SUPER_ADMIN)) {

		} else {
			renderError(404);
			return;
		}

		SqlPara sqlPara = db.getSqlPara("YQShops", kv);
		Page<Record> pages = db.paginate(getPage(), PAGE_SIZE, sqlPara);
		List<Record> list = pages.getList();
		setRainbow(pages);
		setData(list);
		render("/yq/admin/shop/list.html");
	}

	@Override
	public void info() {
		setReferer();
		Long shopId = getParaToLong("shopId");
		YqShop shop = YqShop.dao.findById(shopId);
		if (shop == null) {
			shop = new YqShop();
		}
		setData(shop);
		List<YqApp> apps = YqApp.dao.findAll();
		setData("apps", apps);
		render("/yq/admin/shop/info.html");
	}
	
	
	@Override
	public void delete() {
		renderMsg("暂未开放");
	}

	@Override
	public void submit() {
		YqShopUser user = getShopUser();
		Long shopId = getParaToLong("shopId");
		String name = getPara("name");
		String account = getPara("account");
		String password = getPara("password");
		String intro = getPara("intro");
		String logo = getPara("logo");
		String mobile = getPara("mobile");
		Long appId = getParaToLong("appId");
		if (StrKit.isBlank(mobile)) {
			renderFailure();
			return;
		}
		boolean flag = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {
				YqShop shop = YqShop.dao.findById(shopId);
				if (shop == null) {
					shop = new YqShop();
					shop.setAppId(appId == null ? user.getAppId() : appId);
					shop.print();
					if (!shop.save()) {
						return false;
					}

					YqShopUser info = new YqShopUser();
					info.setAccount(mobile);
					info.setName(name);
					info.setPassword("123456");
					info.setRole(YqShopUser.ROLE_SHOP);
					info.setShopId(shop.getId());
					info.setMobile(mobile);
					info.setStatus(1);
					info.setAppId(appId == null ? user.getAppId() : appId);
					if (!info.save()) {
						return false;
					}
				}
				if (StrKit.isBlank(shop.getIdentifier())) {
					shop.setIdentifier(HASHID.encode(shop.getId()));
				}
				if (StrKit.notBlank(name)) {
					shop.setName(name);
				}
				if (StrKit.notBlank(account)) {
					shop.setAccount(account);
				}
				if (StrKit.notBlank(password)) {
					shop.setPassword(password);
				}
				if (StrKit.notBlank(intro)) {
					shop.setIntro(intro);
				}
				if (StrKit.notBlank(logo)) {
					shop.setLogo(logo);
				}
				if (StrKit.notBlank(mobile)) {
					shop.setMobile(mobile);
				}
				return shop.update();
			}
		});
		renderFlag(flag);
	}

}
